<?php 
defined('RUN_KATANA') or die('Restricted access');
require_once(MODEL_INCLUDE_PATH.'global/DeviceDetect.php'); //make usefull
class KatanaTheme
{
	public $Id;
	public $SiteId;
	public $Type;
	public $ParentId;
	public $Default;
	public $Device; //make usefull
	public $Browser; //make usefull
	public $Version; //make usefull
	public $FixedWidth;
	public $HorisontalPos;
	public $GridSystem;
	public $Bootstrap;
	public $BootstrapURL;
	public $BootstrapThemeURL;
	public $IconSet;
	public $IconSetPrefix;
	public $BorderSite;
	public $BorderContent; 	
	public $Header;
	public $HeaderFixed;
	public $HeaderBorder;
	public $HeaderHFB;
	public $HaderContent;
	public $Footer;
	public $FooterFixed;
	public $FooterBorder;
	public $FooterHFB;
	public $FooterContent;
	public $ColLeft;
	public $ColLeftBorder;
	public $ColLeftHFB;
	public $ColLeftContent; 	
	public $ColRight;
	public $ColRightBorder; 	
	public $ColRightHFB;
	public $ColRightContent; 	
	public $HFB;
	public $ComHFB;
	public $HFBSets;
	
	public $Headers;
	public $Footers;
	public $Borders;
	public $Buttons;
	
	public function KatanaTheme() 
	{ 
		global $katana;
		global $com;
		
		self::setSiteId($katana->GetId());
		self::setId($katana->getThemeID()); 
		self::setValues($com);
		if(defined(BOOTSTRAP) && (BOOTSTRAP))
		{ self::setBootstrap(BOOTSTRAP); }
		if(defined(BOOTSTRAP_URL))
		{ self::setBootstrapURL(BOOTSTRAP_URL); }
		if(defined(BOOTSTRAP_THEME))
		{ self::setBootstrapThemeURL(BOOTSTRAP_THEME); }
		//echo 'ValuesSet';
		//print_r($this);
	}
	
	public function getId() { return $this->Id; }
	public function getSiteId() { return $this->SiteId; }
	
	public function getThemePath() { return SITE_PATH.'templates/sites/'.$this->SiteId.'/themes/'.$this->Id; }
	public function getThemeIncludePath() { return 'templates/sites/'.$this->SiteId.'/themes/'.$this->Id; }
	public function getThemeURL() { return SITE_URL.'templates/sites/'.$this->SiteId.'/themes/'.$this->Id; }
	
	public function getLayoutIncludePath() { return 'templates/sites/'.$this->SiteId.'/layout'; }
	
	public function getFixedWidth() { return $this->FixedWidth; }
	public function getHorisontalPos() { return $this->HorisontalPos; }
	public function getGridSystem() { return $this->GridSystem; }
	public function getBootstrap() { return $this->Bootstrap; }
	public function getBootstrapURL() { return $this->BootstrapURL; }
	public function getBootstrapThemeURL() { return $this->BootstrapThemeURL; }
	public function getIconSet() { return $this->IconSet; }
	public function getIconSetPrefix() { return $this->IconSetPrefix; }
	public function getBorderSite() { return $this->BorderSite; }
	public function getBorderContent() { return $this->BorderContent; }
	
	public function getHeader() { return $this->Header; }
	public function getHeaderFixed() { return $this->HeaderFixed; }
	public function getHeaderBorder() { return $this->HeaderBorder; }
	public function getHeaderHFB() { return $this->HeaderHFB; }
	public function getHaderContent() { return $this->HaderContent; }
	
	public function getFooter() { return $this->Footer; }
	public function getFooterFixed() { return $this->FooterFixed; }
	public function getFooterBorder() { return $this->FooterBorder; }
	public function getFooterHFB() { return $this->FooterHFB; }
	public function getFooterContent() { return $this->FooterContent; }
	
	public function getColLeft() { return $this->ColLeft; }
	public function getColLeftBorder() { return $this->ColLeftBorder; }
	public function getColLeftHFB() { return $this->ColLeftHFB; }
	public function getColLeftContent() { return $this->ColLeftContent; }
	
	public function getColRight() { return $this->ColRight; }
	public function getColRightBorder() { return $this->ColRightBorder; }
	public function getColRightHFB() { return $this->ColRightHFB; }
	public function getColRightContent() { return $this->ColRightContent; }
	
	public function getHFB() { return $this->HFB; }
	public function getComHFB() { return $this->ComHFB; }
	public function getHFBSets() { return $this->HFBSets; }
	
	public function getHeaders() { return $this->Headers; }
	public function getFooters() { return $this->Footers; }
	public function getBorders() { return $this->Borders; }
	public function getButtons() { return $this->Buttons; }
	public function getButton($id=0) 
	{ 
		if(isset($this->Buttons[$id]))
		{ return $this->Buttons[$id]; }
	}
	
	public function setId($val) { $this->Id=$val; }
	public function setSiteId($val) { $this->SiteId=$val; }
	public function setFixedWidth($val) { $this->FixedWidth=$val; }
	public function setHorisontalPos($val) { $this->HorisontalPos=$val; }
	public function setGridSystem($val) { $this->GridSystem=$val; }
	public function setBootstrap($val) { $this->Bootstrap=$val; }
	public function setBootstrapURL($val) { $this->BootstrapURL=$val; }
	public function setBootstrapThemeURL($val) { $this->BootstrapThemeURL=$val; }
	public function setIconSet($val) { $this->IconSet=$val; }
	public function setIconSetPrefix($val) { $this->IconSetPrefix=$val; }
	
	public function setBorderSite($val) { $this->BorderSite=$val; }
	public function setBorderContent($val) { $this->BorderContent=$val; }
	public function setHeader($val) { $this->Header=$val; }
	public function setHeaderFixed($val) { $this->HeaderFixed=$val; }
	public function setHeaderBorder($val) { $this->HeaderBorder=$val; }
	public function setHeaderHFB($val) { $this->HeaderHFB=$val; }
	public function setHaderContent($val) { $this->HaderContent=$val; }
	public function setFooter($val) { $this->Footer=$val; }
	public function setFooterFixed($val) { $this->FooterFixed=$val; }
	public function setFooterBorder($val) { $this->FooterBorder=$val; }
	public function setFooterHFB($val) { $this->FooterHFB=$val; }
	public function setFooterContent($val) { $this->FooterContent=$val; }
	public function setColLeft($val) { $this->ColLeft=$val; }
	public function setColLeftBorder($val) { $this->ColLeftBorder=$val; }
	public function setColLeftHFB($val) { $this->ColLeftHFB=$val; }
	public function setColLeftContent($val) { $this->ColLeftContent=$val; }
	public function setColRight($val) { $this->ColRight=$val; }
	public function setColRightBorder($val) { $this->ColRightBorder=$val; }
	public function setColRightHFB($val) { $this->ColRightHFB=$val; }
	public function setColRightContent($val) { $this->ColRightContent=$val; }
	public function setHFB($val) { $this->HFB=$val; }
	public function setComHFB($val) { $this->ComHFB=$val; }
	public function setHFBSets($val) { $this->HFBSets=$val; }
	
	public function loadActiveHFB()
	{	
		global $db;
		if(($this->BorderSite > 0) && (!($this->Borders[$this->BorderSite])))
		{ $this->Borders[$this->BorderSite] = new ComBorder($this->BorderSite); }
		if(($this->BorderContent > 0) && (!($this->Borders[$this->BorderContent])))
		{ $this->Borders[$this->BorderContent] = new ComBorder($this->BorderContent); }
		if(($this->Header > 0) && ($this->HeaderBorder > 0) && (!($this->Borders[$this->HeaderBorder])))
		{ $this->Borders[$this->HeaderBorder] = new ComBorder($this->HeaderBorder); }
		if(($this->Footer > 0) && ($this->FooterBorder > 0) && (!($this->Borders[$this->FooterBorder])))
		{ $this->Borders[$this->FooterBorder] = new ComBorder($this->FooterBorder); }
		if(($this->ColLeft > 0) && ($this->ColLeftBorder > 0) && (!($this->Borders[$this->ColLeftBorder])))
		{ $this->Borders[$this->ColLeftBorder] = new ComBorder($this->ColLeftBorder); }
		if(($this->ColRight > 0) && ($this->ColRightBorder > 0) && (!($this->Borders[$this->ColRightBorder])))
		{ $this->Borders[$this->ColRightBorder] = new ComBorder($this->ColRightBorder); }
			
		if($this->HFB > 0)
		{ $this->HFBSets[$this->HFB]['id'] = $this->HFB; }
		if($this->ComHFB > 0)
		{ $this->HFBSets[$this->ComHFB]['id'] = $this->ComHFB; }
		if(($this->Header > 0) && ($this->HeaderHFB > 0))
		{ $this->HFBSets[$this->HeaderHFB]['id'] = $this->HeaderHFB; }
		if(($this->Footer > 0) && ($this->FooterHFB > 0))
		{ $this->HFBSets[$this->FooterHFB]['id'] = $this->FooterHFB; }
		if(($this->ColLeft > 0) && ($this->ColLeftHFB > 0))
		{ $this->HFBSets[$this->ColLeftHFB]['id'] = $this->ColLeftHFB; }
		if(($this->ColRight > 0) && ($this->ColRightHFB > 0))
		{ $this->HFBSets[$this->ColRightHFB]['id'] = $this->ColRightHFB; }
		
		if($this->HFBSets)
		{
			foreach($this->HFBSets as $set)
			{
				if($sql == '')
				{ $sql .= ' id='.$set['id']; }
				else
				{ $sql .= ' OR id='.$set['id']; }
				
			}
			$sql  = 'SELECT id, header, footer, border, border2, button FROM '.DB_PERFIX.'hfb_sets WHERE '.$sql.';';
			
			$result = $db->query($sql);
			$data = $db->fetch($sql, CACHE_TTL);
		
			$result = mysql_query($sql);
			if(is_array($data))
			{
				foreach($data as $row)
				{
					foreach(array_keys($row) as $key)
					{ $this->HFBSets[$row['id']][$key] = $row[$key]; }
				}
				
				foreach($this->HFBSets as $set)
				{
					if(($set['header'] > 0) && (!($this->Headers[$set['header']])))
					{ $this->Headers[$set['header']] = new ComHeader($set['header']); }
					
					if(($set['footer'] > 0) && (!($this->Footers[$set['footer']])))
					{ $this->Footers[$set['footer']] = new ComFooter($set['footer']); }
					
					if(($set['border'] > 0) && (!($this->Borders[$set['border']])))
					{ $this->Borders[$set['border']] = new ComBorder($set['border']); }
					if(($set['border2'] > 0) && (!($this->Borders[$set['border2']])))
					{ $this->Borders[$set['border2']] = new ComBorder($set['border2']); }

					if($set['button'] > 0)
					{ $this->Buttons[$set['button']]['id'] = $set['button']; }
				}				
				self::loadActiveHeaders();
				self::loadActiveFooters();
				self::loadActiveButtons();
			}
		}
		self::loadActiveBorders();
	}
	
	public function loadActiveButtons()
	{	
		if(count($this->Buttons) > 0)
		{
			foreach(array_keys($this->Buttons) as $key)
			{
				if($sql == '')
				{ $sql .= ' id='.$key; }
				else
				{ $sql .= ' OR id='.$key; }
				
			}
			if($sql != '')
			{
				global $db;
				$sql .= ' AND active=1';
				$sql  = 'SELECT * FROM '.DB_PERFIX.'buttons WHERE '.$sql.';';
				
				$result = $db->query($sql);
				$data = $db->fetch($sql, CACHE_TTL);
				if(is_array($data))
				{
					foreach($data as $row)
					{
						foreach(array_keys($row) as $key)
						{ $this->Buttons[$row['id']][$key] = $row[$key]; }
					}
				}
			}
		}
	}
	
	public function loadActiveHeaders()
	{	
		if($this->Headers)
		{
			foreach($this->Headers as $ComHeader)
			{
				if($sql == '')
				{ $sql .= ' id='.$ComHeader->getId(); }
				else
				{ $sql .= ' OR id='.$ComHeader->getId(); }
				
			}
			if($sql != '')
			{
				$sql .= ' AND active=1';
				$sql  = 'SELECT id, l_image, image, image2, r_image, height, button, icon, imagefont FROM '.DB_PERFIX.'com_headers WHERE '.$sql.';';
				
				$result = mysql_query($sql);
				if($result)
				{
					while($row = mysql_fetch_array($result))
					{
						$this->Headers[$row['id']]->setId($row['id']);
						$this->Headers[$row['id']]->setImageL($row['l_image']);
						$this->Headers[$row['id']]->setImageR($row['r_image']);
						$this->Headers[$row['id']]->setImage($row['image']);
						$this->Headers[$row['id']]->setImage2($row['image2']);
						$this->Headers[$row['id']]->setHeight($row['image2']);
						$this->Headers[$row['id']]->setButton($row['button']);
						$this->Headers[$row['id']]->setIcon($row['icon']);
						$this->Headers[$row['id']]->setImagefont($row['imagefont']);
						
						if(($this->Headers[$row['id']]->Button > 0) && (!($this->Buttons[$this->Headers[$row['id']]->Button])))
						{ $this->Buttons[$this->Headers[$row['id']]->Button]['id'] = $this->Headers[$row['id']]->Button; }
					}
				}
			}
		}
	}
	
	public function loadActiveFooters()
	{	
		if($this->Footers)
		{
			foreach($this->Footers as $ComFooter)
			{
				if($sql == '')
				{ $sql .= ' id='.$ComFooter->getId(); }
				else
				{ $sql .= ' OR id='.$ComFooter->getId(); }
				
			}
			if($sql != '')
			{
				$sql .= ' AND active=1';
				$sql  = 'SELECT id, l_image, image, image2, r_image, height, button, icon, imagefont FROM '.DB_PERFIX.'com_footers WHERE '.$sql.';';
				
				$result = mysql_query($sql);
				if($result)
				{
					while($row = mysql_fetch_array($result))
					{
						$this->Footers[$row['id']]->setId($row['id']);
						$this->Footers[$row['id']]->setImageL($row['l_image']);
						$this->Footers[$row['id']]->setImageR($row['r_image']);
						$this->Footers[$row['id']]->setImage($row['image']);
						$this->Footers[$row['id']]->setImage2($row['image2']);
						$this->Footers[$row['id']]->setHeight($row['image2']);
						$this->Footers[$row['id']]->setButton($row['button']);
						$this->Footers[$row['id']]->setIcon($row['icon']);
						$this->Footers[$row['id']]->setImagefont($row['imagefont']);
						
						if(($this->Footers[$row['id']]->Button > 0) && (!($this->Buttons[$this->Footers[$row['id']]->Button])))
						{ $this->Buttons[$this->Footers[$row['id']]->Button]['id'] = $this->Footers[$row['id']]->Button; }
					}
				}
			}
		}
	}
	
	public function loadActiveBorders()
	{	
		if($this->Borders)
		{
			foreach($this->Borders as $ComBorder)
			{
				if($where == '')
				{ $where = ' id='.$ComBorder->getId(); }
				else
				{ $where .= ' OR id='.$ComBorder->getId(); }
			}
			if($where != '')
			{
				$sql  = 'SELECT id, type, image_top_l, image_top_r, image_bot_l, image_bot_r, image_mid_l, image_mid_r, active';
				$sql .= ' FROM '.DB_PERFIX.'com_borders WHERE '.$where.' AND active=1;';
				
				$result = mysql_query($sql);
				if($result)
				{
					while($row = mysql_fetch_array($result))
					{
						$this->Borders[$row['id']]->setId($row['id']);
						$this->Borders[$row['id']]->setType($row['type']);
						//$this->Borders[$row['id']]->setWidth($row['width']);
						//$this->Borders[$row['id']]->setImageBg($row['image_bg']);
						//$this->Borders[$row['id']]->setImageTop($row['image_top']);
						$this->Borders[$row['id']]->setImageTopL($row['image_top_l']);
						$this->Borders[$row['id']]->setImageTopR($row['image_top_r']);
						//$this->Borders[$row['id']]->setImageBot($row['image_bot']);
						$this->Borders[$row['id']]->setImageBotL($row['image_bot_l']);
						$this->Borders[$row['id']]->setImageBotR($row['image_bot_r']);
						//$this->Borders[$row['id']]->setImageMidL($row['image_mid_l']);
						//$this->Borders[$row['id']]->setImageMidR($row['image_mid_r']);
						//$this->Borders[$row['id']]->setActive($row['active']);
					}
				}
			}
		}
	}
	
	public function getLayoutClass($zone = '')
	{
		$class = '';
		if(($this->ColLeft > 0) && ($this->ColRight > 0))
		{ $class = 'tripple_col col_left_included col_right_included'; }
		else
		{
			if($this->ColLeft > 0) { $class = 'double_col col_left_included'; }
			if($this->ColRight > 0) { $class = 'double_col col_right_included'; }
		}
		return $class;
	}
	
	public function setValues($com) 
	{ 
		if($this->Id > 0)
		{ $where = 'id='.$this->Id; }
		else
		{ 
			if($this->SiteId > 0)
			{ $where = 'site_id='.$this->SiteId.' ORDER BY site_default DESC'; }
		}
		
		if($where != '')
		{
			$select = 'SELECT * FROM '.DB_PERFIX.'themes WHERE '.$where.' LIMIT 1;';
			$result = mysql_query($select);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					self::setId($row['id']);
					self::setFixedWidth($row['fixed_width']);
					self::setHorisontalPos($row['horisontal_pos']);
					self::setGridSystem($row['GridSystem']);
					self::setBootstrap($row['Bootstrap']);
					self::setBootstrapURL($row['BootstrapURL']);
					self::setBootstrapThemeURL($row['BootstrapThemeURL']);
					self::setIconSet($row['IconSet']);
					self::setIconSetPrefix($row['IconSetPrefix']);
					self::setBorderSite($row['border_site']);
					self::setBorderContent($row['border_content']);
					self::setHeader($row['header']);
					self::setHeaderFixed($row['header_fixed']);
					self::setHeaderBorder($row['header_border']);
					self::setHeaderHFB($row['header_hfb']);
					self::setHaderContent($row['header_content']);
					self::setFooter($row['footer']);
					self::setFooterFixed($row['footer_fixed']);
					self::setFooterBorder($row['footer_border']);
					self::setFooterHFB($row['footer_hfb']);
					self::setFooterContent($row['footer_content']);
					self::setColLeft($row['col_left']);
					self::setColLeftBorder($row['col_left_border']);
					self::setColLeftHFB($row['col_left_hfb']);
					self::setColLeftContent($row['col_left_content']);
					self::setColRight($row['col_right']);
					self::setColRightBorder($row['col_right_border']);
					self::setColRightHFB($row['col_right_hfb']);
					self::setColRightContent($row['col_right_content']);
					self::setHFB($row['hfb_set']);
					self::setComHFB($row['hfb_com_set']);
				}
				/* Component HFB overrides Theme Component HFB-Default */
				if($com->getHFB() > 0)
				{ self::setComHFB($com->getHFB()); }
				else
				{ $com->setHFB(self::getComHFB()); }
				//echo 'ValuesSet';
				//print_r($this);
			}
		}
	}
}
?>